package com.mm.cloud.gateway.filter.white;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 白名单
 *
 * @author mr
 * @since 2021-07-13
 */
@Data
@Component
@ConfigurationProperties(prefix = "ignore")
public class IgnoreWhiteProperties {

    private List<String> white;

    AntPathMatcher matcher = new AntPathMatcher();

    public boolean matches(String url) {
        if (StringUtils.isEmpty(url) || CollectionUtils.isEmpty(white)) {
            return false;
        }
        for (String s : white) {
            if (s.contains("/*") && url.contains(s.replace("/*", ""))) {
                return true;
            }
            if (matcher.match(s, url)) {
                return true;
            }
        }
        return false;
    }

}
